package controllers;


import models.Project;
import models.User;
import models.Log;
import models.Task;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.List;

import play.mvc.With;
import play.mvc.Controller;
/**
 *
 * @author mohamedhisham90
 *
 */
@With(Secure.class)
public class ImpedimentLog extends Controller {
    /**
     *
     * @param projectID
     *           takes the project id from the URL and
     *           views the impediment log of that project
     */
    public static void viewImpedimentLog(final Long projectID) {
        Security.checkAccess("View the impediment log", projectID, Project.class
            .getName());
        List<Log> imptaskLog = impedimentTasksInLog(projectID);
        List<User> userChangedt = User.all().fetch();
        Project pro = Project.findById(projectID);
        Set <Task> impTask = new HashSet<Task>();
        List<Task> allTasks = Task.all().fetch();
        for (int i = 0; i < allTasks.size(); i++) {
            if (allTasks.get(i).type.type.equals("Impediment")) {
               impTask.add(allTasks.get(i));
             }
       }
        List <Task> allImptasks = new LinkedList<Task>();
        allImptasks.addAll(impTask);
        render(imptaskLog, userChangedt, allImptasks, pro);
    }
    /**
     *
     * @param projectID
     *        takes as input the project ID for the log query
     *@return
     *     the list of the tasks changed to impediments
     */
     public static List<Log> impedimentTasksInLog(final Long projectID) {
        Project pro = Project.findById(projectID);
        List<Log> allTLog = Log
        .find(
        "select l from Log l "
           + "where l.artifactType=? and l.projectId=? and l.description=?",
           "Task", pro.getEntityId(), "changed the task type to impediment"
           ).fetch();

        return allTLog;
     }
}
